# coding=utf-8
import requests
import pandas as pd
import numpy as np
import json
from tqdm import tqdm
import time


def get_price(sku, proxy):
    url =  f'https://p.3.cn/prices/mgets?skuIds=J_{sku}&type=1'
    # key_dict = {"query": "python"}
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }
    if proxy == NoProxy:
        response = requests.get(url, headers=headers)
    else:
        response = requests.get(url, proxy=proxy, headers=headers)
    price = json.loads(response.text)[0]['p']
    return price


def read_csv(params):
    csv_file = params['csv_path'][0]
    sleep_sec = int(params['sleep_sec'][0])
    proxy_path = params['proxy_path'][0]
    switch_proxy = SwitchProxy(proxy_path)
    proxy = switch_proxy.get_new()
    data = pd.read_csv(csv_file, encoding='utf-8')

    new_lst, break_flag = [], False
    for index,row in tqdm(data.iterrows()):
        while True:
            try:
                price = get_price(row["SKU"], proxy)
                new_lst.append([row["SKU"], price])
                break
            except:
                proxy = switch_proxy.get_new()
                if proxy is None or proxy == "NoProxy":
                    print("代理池用完，或当前IP不可用，请更换代理池")
                    break_flag = True
                    break
        if break_flag: break
        time.sleep(sleep_sec)
    print(f"已解析：{index}个")
    new_data = pd.DataFrame(new_lst, columns=['sku', 'price'])
    new_data.to_csv(csv_file[:-4]+"_save.csv", index=False)


class SwitchProxy():
    def __init__(self, proxy_file):
        try:
            self.proxy_list = [i.strip() for i in open(proxy_file, 'r').readlines()]
        except:
            self.proxy_list = []
        self.length = len(self.proxy_list)
        self.index = 0


    def get_new(self):
        if self.index == self.length:
            if self.length == 0:
                return "NoProxy"

            return None
        proxy = {
            "https": f"https://{self.proxy_list[self.index]}"
        }
        self.index += 1
        return proxy

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    params = pd.read_csv('配置.csv')
    read_csv(params)

